/* ------------------------------------------------------------------------
	Author: SlopeTracker (http://www.pluginexpert.com)
	Version: 1.0
------------------------------------------------------------------------- */
var stToolBar = {
    loadPage: function(action, data_post, message, callback_function, page_name) {
        var post_data = "ajax=1&action="+action;
        if(page_name == '' || page_name == null){
            page_name = "stToolBar";
        }
        post_data = "page="+page_name+'&'+post_data;
        if(data_post != '' || data_post != null){
            for( var key in data_post){
                if(key == 'sortKey')
                    post_data +="&sort="+data_post[key];
                else
                    post_data +="&"+key+"="+data_post[key];
            }
        }
        //alert(post_data);
        jQuery.ajax({
            type: "POST",
            //url: "admin.php?page=home",
            data: post_data,
            cache: false,
            beforeSend: function(){
                if(jQuery("#loading").css('display') == 'none'){
                    jQuery.blockUI({
                        message: jQuery('#loading'),
                        css: {
                        border: 'none',
                        padding: '15px',
                        backgroundColor: '#000',
                        '-webkit-border-radius': '10px',
                        '-moz-border-radius': '10px',
                        opacity: .5,
                        color: '#fff'
                    }});
                }
                jQuery("#loading").fadeIn("fast");
                jQuery("#inner_content").fadeOut("fast");
            }, //show loading just when link is clicked
            complete: function(){
                jQuery("#loading").fadeOut("fast");
                jQuery.unblockUI();
            }, //stop showing loading when the process is complete
            success: function(data){
                jQuery("#inner_content").html(data);
                jQuery("#inner_content").append('<input type="hidden" value="'+action+'" id="current_action" />');
                if(message){
                    stToolBar.sendMessage(message);
                }
                if(callback_function){
                    callback_function.call(this, data_post);
                }
                jQuery("#inner_content").fadeIn("normal");

            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(jQuery.unblockUI, 2000);
            }
        });
		stToolBar.pagingCallbackFunction = function(data) {};
		stToolBar.sortCallbackFunction = function(data) {};
    },

    load_tooltip: function(id){
        jQuery(document).ready(function() {
            if(id == '')
                jQuery("#inner_content").tooltip();
            else
                jQuery(id).tooltip();
        });
    },

    on_submitform: function(form, callback_function){
        jQuery(document).ready(function(){
                jQuery('.stform input').each(function(){
                    jQuery(this).focus(function(){
                        jQuery(this).parent().addClass('focus');
                    });
                    jQuery(this).blur(function(){
                        jQuery(this).parent().removeClass('focus');
                    });
                });
                jQuery('.stform textarea').each(function(){
                    jQuery(this).focus(function(){
                        jQuery(this).parent().addClass('focus');
                    });
                    jQuery(this).blur(function(){
                        jQuery(this).parent().removeClass('focus');
                    });
                });
                jQuery('.stform select').each(function(){
                    jQuery(this).focus(function(){
                        jQuery(this).parent().addClass('focus');
                    });
                    jQuery(this).blur(function(){
                        jQuery(this).parent().removeClass('focus');
                    });
                });
                jQuery(form).validate({
                    errorPlacement: function(error, element) {
                        //error.appendTo( element.parent());
                        element.parent().addClass('error');
                    },
                    submitHandler: function() {
                        jQuery.blockUI({
                            message: jQuery('#loading'),
                            css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#fff'
                        } });
                        //jQuery("#loading").fadeIn("fast");
                        //jQuery("#inner_content").fadeOut("fast");
                        jQuery.post(jQuery(form).attr('action')+'&ajax=1', jQuery(form).serialize(),function(data, textStatus){
                            if(jQuery.isFunction(callback_function)){
                                callback_function.call(this, data);
                            }else if(typeof(callback_function) == 'undefined'){
                            	stToolBar.sendMessage(data);
                            } else {
                            	stToolBar.sendMessage(data);
                            }
                            if(jQuery("#loading").css('display') != 'none'){
                                jQuery.unblockUI();
                                jQuery("#loading").fadeOut('normal');
                            }
                        });
                    }

                });
        });
    },

    load_main_menu: function(){
        jQuery(document).ready(function(){
            jQuery('#tabs li a').click(function(){
                stToolBar.loadMenuCallbackFunction = function (){};
                var action = jQuery(this).attr('action');
                var plugin_name = jQuery(this).attr('plugin_name');

                jQuery('#tabs ul li').each(function(){
                    var current_action = jQuery(this).children().attr('action');

                    if(current_action == action ){

                        jQuery(this).addClass('current');
                    }else{
                        jQuery(this).removeClass('current');
                    }
                });

                if(jQuery(this).hasClass('have_child')){

                    jQuery('#side-nav ul').each(function(){
                        if(jQuery(this).attr('id') == action){
                            jQuery(this).fadeIn();
                        }else{
                            jQuery(this).css('display','none');
                        }
                    });
                    jQuery('#side-nav ul li').each(function(){
                        if(jQuery(this).children('a').attr('action') == action){
                            jQuery(this).addClass('current');
                        }else{
                            jQuery(this).removeClass('current');
                        }
                    });

                    stToolBar.loadPage(action, null, null, function(){
                        jQuery('#inner_content').css('width','87%');
                        jQuery('#inner_content').css('float','right');
                        jQuery('#side-nav').fadeIn('normal');
                        stToolBar.loadMenuCallbackFunction();
                    }, plugin_name);
                }else{

                    jQuery('#side-nav').fadeOut('normal');
                    stToolBar.loadPage(action, null, null, function(){
                        jQuery('#inner_content').css('width','98%');
                        jQuery('#inner_content').css('float','none');
                        stToolBar.loadMenuCallbackFunction();
                    }, plugin_name);
                }
            });
            //alert(jQuery(window).width());
        });
    },

    load_side_menu: function(){
        jQuery(document).ready(function(){
            jQuery('#side-nav li a').click(function(){
                stToolBar.loadMenuCallbackFunction = function (){};
                var action = jQuery(this).attr('action');
                var plugin_name = jQuery(this).attr('plugin_name');
                stToolBar.loadPage(action, null, null, function(){
                    stToolBar.loadMenuCallbackFunction();
                }, plugin_name);
                jQuery('#side-nav ul li').each(function(){
                    var current_action = jQuery(this).children().attr('action');

                    if(current_action == action ){

                        jQuery(this).addClass('current');
                    }else{
                        jQuery(this).removeClass('current');
                    }
                });
            });
            jQuery('#side-nav li a.have_child').click(function(){
                jQuery('#side-nav').fadeIn();
            });
        });
    },

    sortColumn: function(){
        jQuery(document).ready(function(){
            jQuery('th.manage-column').css('cursor', 'pointer');
            jQuery('th.manage-column').click(function(){
                var current_action = jQuery('#current_action').val();
                var sort_type = jQuery(this).attr('sort_type');
                var sort_id = jQuery(this).attr('id');
                if(sort_type == null){
                    sort_type = 'desc';
                }
                var data_post = new Array();

                stToolBar.sortCallbackFunction(data_post);


                data_post['sortKey'] = sort_id;
                data_post['sort_type'] = sort_type;
                jQuery('#st_pagination ul li').each(function(){
                    if(jQuery(this).hasClass('current')){
                        data_post['cur_page'] = jQuery(this).children().html();
                    }
                });
                stToolBar.loadPage(current_action, data_post, '', function(data_post){
                    if(data_post['sort_type'] == '' || data_post['sort_type'] == 'desc'){
                        jQuery('#'+data_post['sortKey']).attr('sort_type','asc');
                        jQuery('#'+data_post['sortKey']).append('<span class="icon_desc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
                    }else{
                        jQuery('#'+data_post['sortKey']).attr('sort_type','desc');
                        jQuery('#'+data_post['sortKey']).append('<span class="icon_asc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
                    }
                });
            });
        });
    },

    shortAction: function(){
        jQuery(document).ready(function(){
            jQuery('div.short_action span').css('cursor', 'pointer');
            jQuery('div.short_action span').click(function(){
                var current_action = jQuery('#current_action').val();
                var action = jQuery(this).attr('action');
                var short_action = jQuery(this).attr('short_action');
                var id = jQuery(this).attr('id');
                var confirm_message = jQuery(this).attr('confirm');
                var functionCall = jQuery(this).attr('function');
                var data_post = new Array();
                data_post['id'] = id;
                data_post['short_action'] = short_action;

                stToolBar.sortCallbackFunction(data_post);

                if(action){
                    current_action = action;
                }

                if(confirm_message){
                    stToolBar.confirmModal(confirm_message, short_action, data_post, functionCall);

                }else{
                    if(functionCall == '' || functionCall == null)
                        stToolBar.loadPage(current_action, data_post);
                    else
                        eval(functionCall);
                }
            });
        });
    },

    sendMessage: function(data){
        jQuery(document).ready(function(){
            try{
                data = eval('('+data+')');
            }catch (e){}

            if(typeof(data)=='string'){
                var message = data;
                var status = 'message';
            }else{
                message = data['message'];
                status = data['status'];
            }
            if(status == 'success'){
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).addClass('notification_success');
                jQuery('.notification .text strong').html('Success!');
            }else if(status == 'message'){
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_message');
                jQuery('.notification .text strong').html('Message!');
            }else{
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_error');
                jQuery('.notification .text strong').html('Error!');
            }
            jQuery('.notification .text span').html(message);
            jQuery('.notification').fadeIn('slow');

            jQuery('.notification').click(function() {
                jQuery(this).fadeOut(800);
            });
            window.setTimeout(function(){
               jQuery('.notification').fadeOut();
            }, 2500);
        });

    },

    paging: function(page_num, action, callback_function){
        var data_post = new Array();
        data_post['cur_page'] = page_num;

        jQuery('th.manage-column').each(function(){
            if(jQuery(this).attr('sort_type')){
                //alert(jQuery(this).attr('sort_type'));
                data_post['sort'] = jQuery(this).attr('id');
                if(jQuery(this).attr('sort_type') == 'desc')
                    data_post['sort_type'] = 'asc';
                else
                    data_post['sort_type'] = 'desc';
            }
        });

        stToolBar.pagingCallbackFunction(data_post);

        stToolBar.loadPage(action, data_post, '', function(data_post){
            if(data_post['sort_type'] != null){
                if(data_post['sort_type'] == '' || data_post['sort_type'] == 'desc'){
                    jQuery('#'+data_post['sort']).attr('sort_type','asc');
                    jQuery('#'+data_post['sort']).append('<span class="icon_desc">&nbsp;&nbsp;&nbsp;</span>');
                }else{
                    jQuery('#'+data_post['sort']).attr('sort_type','desc');
                    jQuery('#'+data_post['sort']).append('<span class="icon_asc">&nbsp;&nbsp;&nbsp;</span>');
                }
            }
        });
    },
    pagingCallbackFunction: function(data){},
    sortCallbackFunction: function(data){},

    loadUploadButton: function(thumbnail_width, action, callBackfunction){
        if(!action){
            action = 'ajaxPostAction';
        }
        if(!thumbnail_width){
            thumbnail_width = '';
        }
        jQuery(document).ready(function(){
            jQuery('.image_upload_button').each(function(){
                var clickedObject = jQuery(this);
                var clickedID = jQuery(this).attr('id');
                new AjaxUpload(clickedID, {
                    action: location.href,
                    name: clickedID, // File upload name
                    data: { // Additional data to send
                        action: action,
                        ajax: 1,
                        type: 'upload',
                        data: clickedID,
                        thumbnail_width: thumbnail_width
                    },
                    autoSubmit: true, // Submit file after selection
                    responseType: false,
                    onChange: function(file, extension){},
                    onSubmit: function(file, extension){
                        clickedObject.text('Uploading'); // change button text, when user selects file
                        this.disable(); // If you want to allow uploading only 1 file at time, you can disable upload button
                        interval = window.setInterval(function(){
                            var text = clickedObject.text();
                            if (text.length < 13){clickedObject.text(text + '.');}
                            else {clickedObject.text('Uploading');}
                        }, 200);
                    },
                    onComplete: function(file, response) {
                        response = response.split('|*|');
                        var file_url = response[0];
                        var file = response[1];
                        window.clearInterval(interval);
                        clickedObject.text('Choose picture');
                        this.enable(); // enable upload button

                        // If there was an error
                        if(file_url.search('Upload Error') > -1){
                            var buildReturn = '<span class="upload-error">' + file_url + '</span>';
                            jQuery(".upload-error").remove();
                            clickedObject.parent().after(buildReturn);

                        }
                        else{
                        	if(jQuery.isFunction(callBackfunction)){
                        		callBackfunction(thumbnail_width, file_url);
                        	} else {
	                            var buildReturn = '<img class="hide preview" id="image_'+clickedID+'" src="'+file_url+'" alt="" />';

	                            jQuery(".upload-error").remove();
	                            jQuery("#image_" + clickedID).remove();
	                            clickedObject.parent().after(buildReturn);
	                            jQuery('img#image_'+clickedID).fadeIn();
	                            clickedObject.next('span').fadeIn();
	                            clickedObject.parent().prev('input').val(file);
                        	}
                        }
                    }
                });

            });
        });
    },
    loadRemoveButton: function(action){
        if(!action){
            action = 'ajaxPostAction';
        }
        jQuery(document).ready(function(){
            jQuery('.image_reset_button').click(function(){
                var clickedObject = jQuery(this);
                var theID = jQuery(this).attr('pictureID');

                var ajax_url = location.href;

                var data = {
                    action: action,
                    ajax: 1,
                    type: 'image_remove',
                    data: theID,
                    file: jQuery('#id_'+theID).val()
                };

                jQuery.post(ajax_url, data, function(response) {
                    var image_to_remove = jQuery('#image_' + theID);
                    var button_to_hide = jQuery('#reset_' + theID);
                    image_to_remove.fadeOut(500,function(){jQuery(this).remove();});
                    button_to_hide.fadeOut();
                    clickedObject.parent().prev('input').val('');
                });
                return false;

            });
        });
    },

    showSideMenu: function(action){
        jQuery(document).ready(function(){
            jQuery('#side-nav').fadeIn();
            jQuery('#side-nav ul').each(function(){
                jQuery(this).css('display','none');
            });
            jQuery('#side-nav ul li').each(function(){
                if(jQuery(this).children('a').attr('action') == action){
                    jQuery(this).addClass('current');
                    jQuery(this).parent('ul').fadeIn();
                }else{
                    jQuery(this).removeClass('current');
                }
            });

            jQuery('#inner_content').css('width','87%');
            jQuery('#inner_content').css('float','right');
        });
    },
    confirmModal: function(message, btnText, data_post, callbackFunction){
        jQuery( "#dialog-confirm" ).html("<p>"+message+"</p>");
        jQuery( "#dialog-confirm" ).dialog({
            title: 'Confirm',
            resizable: false,
            modal: true,
            show: "slide",
            hide: "slide",
            buttons: [
            {
                text: btnText,
                click: function() {
                    jQuery( this ).dialog( "close" );
                    var current_action = jQuery('#current_action').val();
                    if(callbackFunction == '' || callbackFunction == null)
                        stToolBar.loadPage(current_action, data_post);
                    else
                        eval(callbackFunction);
                }
            },
            {
                text: "Cancel",
                click: function() {
                    jQuery( this ).dialog( "close" );
                }
            }
            ]
        });
    },

    switchButton: function(){
        jQuery(document).ready( function(){
		jQuery(".cb-enable").click(function(){
			var parent = jQuery(this).parents('.switch');
			jQuery('.cb-disable',parent).removeClass('selected');
			jQuery(this).addClass('selected');
			jQuery('.checkbox',parent).attr('checked', true);
                        var data_post = new Array();
                        data_post['handle'] = 1;
                        data_post['plugin_name'] = jQuery(this).attr('name');
                        data_post['value'] = 1;
                        var current_action = jQuery('#current_action').val();
                        stToolBar.loadPage(current_action, data_post);
		});
		jQuery(".cb-disable").click(function(){
			var parent = jQuery(this).parents('.switch');
			jQuery('.cb-enable',parent).removeClass('selected');
			jQuery(this).addClass('selected');
			jQuery('.checkbox',parent).attr('checked', false);

                        var data_post = new Array();
                        data_post['handle'] = 1;
                        data_post['plugin_name'] = jQuery(this).attr('name');
                        data_post['value'] = 0;
                        var current_action = jQuery('#current_action').val();
                        stToolBar.loadPage(current_action, data_post);
		});
	});
    },
    loadMenuCallbackFunction: function(data){}
};